package com.shine.icis.action;

import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.icis.blo.IDataCheckService;
import com.shine.icis.blo.IDataSubmitService;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.ProgressStatus;
import com.shine.icis.hbean.DataCheckDef;
import com.shine.icis.hbean.DataCheckR12;
import com.shine.icis.hbean.R12ChkItem;

public class DataCheck12Action extends BaseAction {

	/**
	 * 客户资金结算勾稽核对
	 * @author zhangxr 2010-10-27
	 * */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private IDataCheckService dataCheckService;
	private IDataSubmitService dataSubmitService;
	
	// input param
	private int pageNo;
	private int pageSize;
	private int id;
	private Integer relateId;
	private double accSumAmt;//客户资金账户汇总余额
	private double bankAmt;//客户资金专用银行存款
	private double reservesAmt;//客户结算备付金
	private double bailA13Amt;//客户交易保证金(A13)
	private double bailSdcAmt;//客户交易保证金(中登)
	private double arapAmt;//应收应付金额
	private double settleAmt;//应交收金额(不含三板数据）
	private double fullComm;//证券公司向客户扣收的手续费
	private double bankInterest;//证券公司客户资金存款账户收到的利息
	private double sdcCommAmt;//证券公司支付登记公司的一级费用(不含三板数据）
	private double secInterest;//证券公司向客户支付的结息金额
	private double bankComm;//银行收取的手续费
	private double sqtClearAmt;//清算金额（三板）
	private double sqtStamptax;//印花税（三板）
	private double discAmt;//挪用金额
	private String note;//备注
	private String busiDate;//数据日期
	private String expType;//导出方式
	
	//output value
	private DataCheckR12 checkR12;//R12查询结果
	private List<R12ChkItem> chkItems; 
	private ListPage<DataCheckR12> checkR12Page;//R08查询结果
	private List<DataCheckDef> dataCheckDefs;
	private String currentBusiDate;//当前交易日期
	private int jobStatus;//数据核对的状态：-2未就绪；-1等待；0运行中；1结束；2结束但TS处于运行中
	private String downloadUrl;//下载地址
	
	public void setDataCheckService(IDataCheckService dataCheckService) {
		this.dataCheckService = dataCheckService;
	}
	public void setDataSubmitService(IDataSubmitService dataSubmitService) {
		this.dataSubmitService = dataSubmitService;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setRelateId(int relateId) {
		this.relateId = relateId;
	}
	public void setAccSumAmt(double accSumAmt) {
		this.accSumAmt = accSumAmt;
	}
	public void setBankAmt(double bankAmt) {
		this.bankAmt = bankAmt;
	}
	public void setReservesAmt(double reservesAmt) {
		this.reservesAmt = reservesAmt;
	}
	public void setBailA13Amt(double bailA13Amt) {
		this.bailA13Amt = bailA13Amt;
	}
	public void setBailSdcAmt(double bailSdcAmt) {
		this.bailSdcAmt = bailSdcAmt;
	}
	public void setArapAmt(double arapAmt) {
		this.arapAmt = arapAmt;
	}
	public void setSettleAmt(double settleAmt) {
		this.settleAmt = settleAmt;
	}
	public void setFullComm(double fullComm) {
		this.fullComm = fullComm;
	}
	public void setBankInterest(double bankInterest) {
		this.bankInterest = bankInterest;
	}
	public void setSdcCommAmt(double sdcCommAmt) {
		this.sdcCommAmt = sdcCommAmt;
	}
	public void setSecInterest(double secInterest) {
		this.secInterest = secInterest;
	}
	public void setBankComm(double bankComm) {
		this.bankComm = bankComm;
	}
	public void setSqtClearAmt(double sqtClearAmt) {
		this.sqtClearAmt = sqtClearAmt;
	}
	public void setSqtStamptax(double sqtStamptax) {
		this.sqtStamptax = sqtStamptax;
	}
	public void setDiscAmt(double discAmt) {
		this.discAmt = discAmt;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public void setBusiDate(String busiDate) {
		this.busiDate = busiDate;
	}
	public void setExpType(String expType) {
		this.expType = expType;
	}
	
	public DataCheckR12 getCheckR12() {
		return checkR12;
	}
	public List<R12ChkItem> getChkItems() {
		return chkItems;
	}
	public ListPage<DataCheckR12> getCheckR12Page() {
		return checkR12Page;
	}
	public List<DataCheckDef> getDataCheckDefs() {
		return dataCheckDefs;
	}
	public String getCurrentBusiDate() {
		return currentBusiDate;
	}
	public int getJobStatus() {
		return jobStatus;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}
	
	/**
	 * 进入查询页面
	 * @return
	 */
	public String initQueryCheckR12() {
		try {
			dataCheckDefs = dataCheckService.getDataCheckDefs(DataCheckDef.ON);
			currentBusiDate = dataSubmitService.getBusiDate();
			
			ProgressStatus proSta = dataSubmitService.getProgressStatus();
			jobStatus = proSta.getDataCheckStatus();
			
			checkR12 = dataCheckService.getDataCheckR12();
			chkItems = dataCheckService.getManuChkItems();
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	public String queryCheckR12() {
		QueryParameters qp = new QueryParameters();
		
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		//排序信息
		qp.addOrder("id", false);
		//参数
		if(busiDate != null && !busiDate.trim().equals("")){
			qp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		}
		
		try {
			checkR12Page = dataCheckService.getDataCheckR12Page(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 导出Excel
	 */
	public String exportCheckR12() {
		QueryParameters qp = new QueryParameters();
		//排序信息
		qp.addOrder("id", false);
		//参数
		if(busiDate != null && !busiDate.trim().equals("")){
			qp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		}
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = dataCheckService.csExpDataCheckR12(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
	
	/**
	 * 修改备注
	 * @return
	 */
	public String modifyCheckR12Note() {
		try {
			DataCheckR12 r12 = dataCheckService.modifyDataCheckR12Note(id, relateId, accSumAmt, bankAmt, reservesAmt, bailA13Amt, 
					bailSdcAmt, arapAmt, settleAmt, fullComm, bankInterest, sdcCommAmt, secInterest, bankComm, 
					sqtClearAmt, sqtStamptax, discAmt, note);
			return success(r12.getId().toString());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
}
